#include "precompiled.h"
#include <iostream>
#include <io.h>
#include <fcntl.h>
#include "Util.h"
#include "Config.h"
#include "NatcarSimViz.h"
#include "ViacarTeam2.h"
#include "Robomag.h"


int WINAPI WinMain(HINSTANCE	hInstance,			// Instance
				   HINSTANCE	hPrevInstance,		// Previous Instance
				   LPSTR		lpCmdLine,			// Command Line Parameters
				   int			nCmdShow)			// Window Show State
{
	Util::RedirectIOToConsole();

	try {
		// load configuration file before anything gets constructed
		LPWSTR *argv;
		int argc;
		argv = CommandLineToArgvW(GetCommandLineW(), &argc);
		if(argc > 1) {
			size_t count;
			char buf[BUFSIZ];
			wcstombs_s(&count, buf, sizeof(buf), argv[1], _TRUNCATE);
			Config::Load(buf);
		}
		LocalFree(argv);
		
		ViacarTeam2 robot;
		// run the simulation.
		Environment env(&robot);
		std::cout << "Running simulation\n";
		env.RunSimulation();
		std::cout << "\nDone.\n";

		//visualize the result
		NatcarSimViz viz(env);
		viz.Run(hInstance);
	} catch(const std::exception &e) {
		std::cerr << e.what() << std::endl;
	}

	system("pause");

	return 0;
}